﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Automation.Model;
using System.Reflection;

namespace SerializableEditor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string path = @"D:\cloud\program\c#\SerializableEditor\SerializableEditor\ExternalProjYss.db";
            var obj = SerializableObject.UnSerializable(path);
            
            Common.GetType(obj,dataGridView1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //string path = @"D:\cloud\program\c#\SerializableEditor\SerializableEditor\ExternalProjYss.db";
            //Common.data = SerializableObject.UnSerializable(path);
            Common.GetType(Common.data, dataGridView1);
            
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            for (int i = 0; i < e.RowCount; i++)
            {
                dataGridView1.Rows[e.RowIndex + i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridView1.Rows[e.RowIndex + i].HeaderCell.Value = (e.RowIndex + i + 1).ToString();
            }

            for (int i = e.RowIndex + e.RowCount; i < this.dataGridView1.Rows.Count; i++)
            {
                dataGridView1.Rows[i].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var Rowindex = e.RowIndex;
            var Colindex = e.ColumnIndex;
            var value = this.dataGridView1.Rows[Rowindex].Cells[Colindex].Value;
            Common.data=value;
            if (this.dataGridView1.Rows[Rowindex].Cells[Colindex].ReadOnly==true)
            {
                Form2 f = new Form2();
                f.ShowDialog(); 
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            var filename = (System.String[])e.Data.GetData(DataFormats.FileDrop,true);
            string path = filename[0];
            object obj=null;
            try
            {
                obj = SerializableObject.UnSerializable(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            Common.GetType(obj, dataGridView1);
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else e.Effect = DragDropEffects.None;
        }

        private void dataGridView1_DragDrop(object sender, DragEventArgs e)
        {
            var filename = (System.String[])e.Data.GetData(DataFormats.FileDrop, true);
            string path = filename[0];
            object obj = null;
            try
            {
                obj = SerializableObject.UnSerializable(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            Common.GetType(obj, dataGridView1);
        }

        private void dataGridView1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else e.Effect = DragDropEffects.None;
        }
    }
}
